package com.platform.novel.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.annotation.IgnoreAuth;
import com.platform.entity.CategoryEntity;
import com.platform.entity.FootprintEntity;
import com.platform.entity.GoodsEntity;
import com.platform.entity.ProductEntity;
import com.platform.entity.UserEntity;
import com.platform.service.CategoryService;
import com.platform.service.CollectService;
import com.platform.service.FootprintService;
import com.platform.service.GoodsReadPermissionService;
import com.platform.service.GoodsService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.R;

import io.swagger.annotations.Api;
import jodd.util.StringUtil;

@Api(tags = "novel")
@RestController
@RequestMapping("/apin/novel")
public class ApiNovelController extends ApiBaseAction{
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FootprintService footprintService;
	@Autowired
	private CollectService collectService;
    @Autowired
    private GoodsReadPermissionService goodsReadPermissionService;

    /**
     *  list 查看列表
     */
    @IgnoreAuth
    @RequestMapping("/category/{categoryId}")
    public R category(
    		@PathVariable("categoryId") Integer categoryId,
    		@RequestParam Map<String, Object> map) {
    	Integer currentUserId = super.getCurrentUserId();
        CategoryEntity category = categoryService.queryObject(categoryId,currentUserId);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("categoryId", categoryId);
        params.put("isDelete", 0);
        params.put("isOnSale", 1);
        params.put("sidx", "sort_order");
        params.put("order", "desc");
        
        if(map.get("order") != null) {
            params.put("order", map.get("order"));
        }
        if(map.get("brandId") != null) {
            params.put("brandId", map.get("brandId"));
        }
        if(map.get("offset") != null) {
            params.put("offset", Integer.valueOf(map.get("offset").toString()));
        }else {
            params.put("offset", 0);
        }
        if(map.get("limit") != null) {
            params.put("limit", Integer.valueOf(map.get("limit").toString()));
        }else {
            params.put("limit", 6);
        }
        List<GoodsEntity> articles = goodsService.queryList(params);
        return R.ok().put("articles", articles).put("category",category);
    }
    
    
    /**
 	* a  文章、音频、视频等权限类产品查看
 	*/
	@IgnoreAuth
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id,HttpServletRequest request) {
		UserEntity currentUser = super.getCurrentUser();
	    GoodsEntity goods = goodsService.readArticle(id,currentUser);
	    R result = R.ok().put("goods", goods);
	    return result;
	}
	
    /**
 	* 下一章节
 	*/
	@IgnoreAuth
	@RequestMapping("/next/{id}")
	public R next(@PathVariable("id") Integer id,HttpServletRequest request) {
		UserEntity currentUser = super.getCurrentUser();
	    GoodsEntity goods = goodsService.nextArticle(id,currentUser);
	    R result = R.ok().put("goods", goods);
	    return result;
	}
    
    /**
 	* 下一章节
 	*/
	@IgnoreAuth
	@RequestMapping("/last/{id}")
	public R last(@PathVariable("id") Integer id,HttpServletRequest request) {
		UserEntity currentUser = super.getCurrentUser();
	    GoodsEntity goods = goodsService.lastArticle(id,currentUser);
	    R result = R.ok().put("goods", goods);
	    return result;
	}
}
